package com.missilike.api.controller;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.missilike.biz.IMilOrderService;
import com.missilike.core.result.CommonResult;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.request.OrderForm;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: starmile
 * @Description:
 * @Create: 2019-01-02 23:23
 **/
@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    private IMilOrderService milOrderService;

    @ApiOperation(value = "prePay", notes = "发起支付")
    @RequestMapping(value = "/prePay", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult prePay(@RequestBody OrderForm request) {
        log.info("Start to execute /api/order/prePay body request={}", JSON.toJSON(request));
        String result = milOrderService.prePay(request);
        log.info("Completed execute /api/order/prePay responese={}", JSON.toJSON(request));
        return !StringUtils.isBlank(result) ? ResultUtils.ok(result) : ResultUtils.error();
    }

    @ApiOperation(value = "alipayNotify", notes = "支付宝支付回调接口")
    @RequestMapping(value = "/alipayNotify", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult alipayNotify(HttpServletRequest request, HttpServletResponse response){
        log.info("Start to execute /api/order/alipayNotify body request={}", request);
        Boolean result = milOrderService.alipayNotify(request);
        log.info("Start to execute /api/order/alipayNotify responese={}", JSON.toJSON(result));
        return ResultUtils.ok(result);
    }

    @ApiOperation(value = "cashWithdrawal", notes = "账号提现")
    @RequestMapping(value = "/cashWithdrawal", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public CommonResult cashWithdrawal(@RequestBody OrderForm request) throws AlipayApiException {
        log.info("Start to execute /api/order/cashWithdrawal body request={}", JSON.toJSON(request));
        Boolean result = milOrderService.cashWithdrawal(request);
        log.info("Completed execute /api/order/cashWithdrawal responese={}", JSON.toJSON(request));
        return result ? ResultUtils.ok(result) : ResultUtils.error();
    }
}